package com.jar.exercise.java8新特性.方法的引用;

import org.junit.Test;

import java.util.Arrays;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @author ZhangShengHao
 * @className Demo1
 * @description TODO
 * @date 2020/1/27 21:11
 */
public class Demo {
	@Test
	public void test1() {
		Employee employee = new Employee("丽丽", "女", 13);
		/**
		 * Supplier是一个接口，employee的getName方法正好实现了这个接口
		 * 直接拿来，叫做方法的引用
		 */
		Supplier<String> s = employee::getName;
		System.out.println(s.get());
	}

	@Test
	public void test2() {
		Supplier<Employee> e = new Supplier<Employee>() {
			@Override
			public Employee get() {
				return new Employee();
			}
		};

		Supplier<Employee> e1 = Employee::new;
		System.out.println(e1.get());
	}

	@Test
	public void test3() {

		Function<Integer, String[]> function = new Function<Integer, String[]>() {
			@Override
			public String[] apply(Integer integer) {
				return new String[integer];
			}
		};


		System.out.println("---------------------------------");
		Function<Integer, String[]> function1 = integer -> new String[integer];
		String[] apply = function1.apply(5);
		System.out.println(Arrays.toString(apply));


		System.out.println("---------------------------------");
		Function<Integer, String[]> function2 = String[]::new;
		String[] apply1 = function2.apply(5);
		System.out.println(Arrays.toString(apply));
	}
}
